package Service;

import java.awt.List;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Dao.*;
import Entity.*;

/**
 * Servlet implementation class queryCourses
 */
@WebServlet("/queryCourses")
public class queryCourses extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public queryCourses() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		try {
			if(request.getParameter("date")==null){
				date=new Date();
			}
			else{
				date = simpleDateFormat.parse(request.getParameter("date"));
			}
			
			
			//System.out.println(date.toString());
			scheduledao scd=new scheduledao();
			coursedao cod=new coursedao();
			userdao usd=new userdao();
			ArrayList<Boolean> r = new ArrayList<Boolean>();
			ArrayList<Course> ri = new ArrayList<Course>();
			ArrayList<String> rin = new ArrayList<String>();
			r.add(0,new Boolean(false));
			r.add(1,new Boolean(false));
			r.add(2,new Boolean(false));
			r.add(3,new Boolean(false));
			r.add(4,new Boolean(false));
			r.add(5,new Boolean(false));
			for(int i=1;i<=5;i++){
				r.add(i*5+1,new Boolean(scd.querySchedule("1001",new java.sql.Date(date.getTime()),i)));
				r.add(i*5+2,new Boolean(scd.querySchedule("1002",new java.sql.Date(date.getTime()),i)));
				r.add(i*5+3,new Boolean(scd.querySchedule("1003",new java.sql.Date(date.getTime()),i)));
				r.add(i*5+4,new Boolean(scd.querySchedule("1004",new java.sql.Date(date.getTime()),i)));
				r.add(i*5+5,new Boolean(scd.querySchedule("1005",new java.sql.Date(date.getTime()),i)));
			}
			ri.add(0,new Course());
			ri.add(1,new Course());
			ri.add(2,new Course());
			ri.add(3,new Course());
			ri.add(4,new Course());
			ri.add(5,new Course());
			rin.add(0,new String(" "));
			rin.add(1,new String(" "));
			rin.add(2,new String(" "));
			rin.add(3,new String(" "));
			rin.add(4,new String(" "));
			rin.add(5,new String(" "));
			for(int i=1;i<=5;i++){
				if(r.get(i*5+1).booleanValue()){
					ri.add(i*5+1,cod.queryCourseInfo("1001",new java.sql.Date(date.getTime()),i));
					System.out.println(i+" "+(i*5+1)+" "+ri.get(i*5+1).getId());
					System.out.println(usd.findUser(ri.get(i*5+1).getId()).getUname());
					rin.add(i*5+1,usd.findUser(ri.get(i*5+1).getId()).getUname());
					System.out.println(rin.get(i*5+1));
				}
				else{
					ri.add(i*5+1,null);
					rin.add(i*5+1,null);
				}
				if(r.get(i*5+2).booleanValue()){
					
					ri.add(i*5+2,cod.queryCourseInfo("1002",new java.sql.Date(date.getTime()),i));
					rin.add(i*5+2,usd.findUser(ri.get(i*5+2).getId()).getUname());
				}
				else{
					ri.add(i*5+2,null);
					rin.add(i*5+2,null);
				}
				if(r.get(i*5+3).booleanValue()){
					ri.add(i*5+3,cod.queryCourseInfo("1003",new java.sql.Date(date.getTime()),i));
					rin.add(i*5+3,usd.findUser(ri.get(i*5+3).getId()).getUname());
				}
				else{
					ri.add(i*5+3,null);
					rin.add(i*5+3,null);
				}
				if(r.get(i*5+4).booleanValue()){
					ri.add(i*5+4,cod.queryCourseInfo("1004",new java.sql.Date(date.getTime()),i));
					rin.add(i*5+4,usd.findUser(ri.get(i*5+4).getId()).getUname());
				}
				else{
					ri.add(i*5+4,null);
					rin.add(i*5+4,null);
				}
				if(r.get(i*5+5).booleanValue()){
					ri.add(i*5+5,cod.queryCourseInfo("1005",new java.sql.Date(date.getTime()),i));
					rin.add(i*5+5,usd.findUser(ri.get(i*5+5).getId()).getUname());
				}
				else{
					ri.add(i*5+5,null);
					rin.add(i*5+5,null);
				}
			}
			
			
			request.setAttribute("r",r);
			request.setAttribute("ri",ri);
			request.setAttribute("rin",rin);
			HttpSession session = request.getSession();
			User user=(User)session.getAttribute("user");
			int Utype=user.getUtype();
			if(Utype==1||Utype==2){
			request.getRequestDispatcher("class_total.jsp").forward(request,response);
			}else if(Utype==3){
				request.getRequestDispatcher("s_query.jsp").forward(request,response);
			}
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
